﻿package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
    import flash.display.GradientType;
	import flash.display.MovieClip;
    import flash.display.SpreadMethod;
    import flash.display.Sprite;
	import flash.events.Event;
    import flash.filters.BitmapFilter;
    import flash.filters.DisplacementMapFilter;
    import flash.filters.DisplacementMapFilterMode;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.text.TextField;
    
    public class as3displace extends Sprite {
        private var bgColor:uint     = 0xFFCC00;
        private var size:uint        = 200;
        private var offset:uint      = 90;
        private var labelText:String = "Watch the text bend with the displacement map";
		
		private var pntXStart:Number = 50;
		private var pntYStart:Number = 10;
		
		private var pntX:Number = 101;
		private var pntY:Number = 10;
		private var bmpToDistort:Bitmap;
		
		private var blackHoleCentre:Sprite = new Sprite();
		
		private var xMov:Number = 0.5;
		private var yMov:Number = 0.2;
		
        public function as3displace() {
		
			/* see what we're drawing */
			//testing nly
			//createBitmapData();
			
			/* get the source image to distort */
			var bmpDataToDistort:BitmapData = new BigSpaceTile(0, 0);
			bmpToDistort = new Bitmap(bmpDataToDistort);
			addChild(bmpToDistort)
			

			blackHoleCentre = new BlackHoleCentre();
			addChild(blackHoleCentre);
			
			
			addEventListener(Event.ENTER_FRAME, enterFrameFunc);
        }
		
		private function enterFrameFunc(e:Event):void 
		{
			pntX += xMov;
			pntY += yMov;
			
			if (pntX >= stage.stageWidth - 350|| pntX < pntXStart) xMov *= -1;
			if (pntY >= stage.stageHeight - 500 || pntY < pntYStart) yMov *= -1;
			
			
			var xOffset:Number = 200;
			var yOffset:Number = 200;
			
            var myFilters:Array = createFilter();
			bmpToDistort.filters = myFilters;
			blackHoleCentre.x = pntX + xOffset;
			blackHoleCentre.y = pntY + yOffset;
				
		}
		
		
		
        private function createFilter():Array {
            var filter:BitmapFilter = getBitmapFilter();
            return new Array(filter);
        }
		
        private function getBitmapFilter():BitmapFilter {
            var mapBitmap:BitmapData = createBitmapData();
			
            var mapPoint:Point       = new Point(pntX, pntY);
            var channelRed:uint      = BitmapDataChannel.RED;
            var channelGreen:uint    = BitmapDataChannel.GREEN;
            var channelBlue:uint     = BitmapDataChannel.BLUE;
            var componentX:uint      = channelBlue;
            var componentY:uint      = channelRed;
            var scaleX:Number        = 150;
            var scaleY:Number        = 150;
            var mode:String          = DisplacementMapFilterMode.CLAMP;
            var color:uint           = 0;
            var alpha:Number         = 0;
            return new DisplacementMapFilter(mapBitmap,
                                             mapPoint,
                                             componentX,
                                             componentY,
                                             scaleX,
                                             scaleY,
                                             mode,
                                             color,
                                             alpha);
        }
		
		
		
        private function createBitmapData():BitmapData {
			var filterMC:MovieClip = new Filter();
			
			
			/* create the 'neutral' bitmap to start with  
			 * 
			 * '80' is the neutral value for each channel
			 * 
			 * */
            var bitmapData:BitmapData = new BitmapData(filterMC.width, filterMC.height, true, 0x80808080 );
            bitmapData.draw(filterMC);
            //var bitmap:Bitmap = new Bitmap(bitmapData);
			
           
			//addChild(bitmap);
            return bitmapData;
        }
     
    }
}
